package com.kinptm.masterclock.utils;

import com.kinptm.masterclock.bean.SpeedTime;

/**
 * Created by pc on 13-11-8.
 */
public class TimeUtil {
    private static final int SECOND = 1000;
    private static final int MINUTE = SECOND * 60;
    private static final int HOUR = MINUTE * 60;
    private static final int DAY = HOUR * 24;

    public static void transform2Time(long consumeTime, SpeedTime time) {
        int day = (int) (consumeTime / DAY);
        int hourTimeMillis = (int) (consumeTime % DAY);

        int hour = hourTimeMillis / HOUR;
        int minuteTimeMillis = hourTimeMillis % HOUR;
        int minute = minuteTimeMillis / MINUTE;
        int secondTimeMillis = minuteTimeMillis % MINUTE;
        int second = secondTimeMillis / SECOND;
        int ms = secondTimeMillis % SECOND;
        int millis = ms >= 100 ? ms / 10 : ms;
        time.setDay(day);
        time.setHour(hour);
        time.setMinute(minute);
        time.setSecond(second);
        time.setMillis(millis);
    }

    public static long transform2Millis(SpeedTime time) {
        int dayMillis = time.getDay() * DAY;
        int hourMillis = time.getHour() * HOUR;

        int minuteMillis = time.getMinute() * MINUTE;
        int secondMillis = time.getSecond() * SECOND;
        int millis = time.getMillis();

        return dayMillis + hourMillis + minuteMillis + secondMillis + millis;
    }

    public static String formatTime(SpeedTime time) {

        return String.format("%s:%s:%s.%s", keepTwoNum(time.getHour()), keepTwoNum(time.getMinute()), keepTwoNum(time.getSecond()), keepTwoNum(time.getMillis()));
    }

    public static String transform2TimeAndFormat(long consumeTime) {
        SpeedTime time = new SpeedTime();
        transform2Time(consumeTime, time);
        return formatTime(time);
    }

    public static String keepTwoNum(int num) {
        return num < 10 ? "0" + num : String.valueOf(num);
    }
}
